#!/bin/sh
#
# setup configfs for adbd, usb mass storage and MTP....
#

UMS_EN=off
ADB_EN=off
MTP_EN=off
NTB_EN=off
ACM_EN=off

USB_ATTRIBUTE=0x409
USB_GROUP=rockchip
USB_SKELETON=b.1

CONFIGFS_DIR=/sys/kernel/config
USB_CONFIGFS_DIR=${CONFIGFS_DIR}/usb_gadget/${USB_GROUP}
USB_STRINGS_DIR=${USB_CONFIGFS_DIR}/strings/${USB_ATTRIBUTE}
USB_FUNCTIONS_DIR=${USB_CONFIGFS_DIR}/functions
USB_CONFIGS_DIR=${USB_CONFIGFS_DIR}/configs/${USB_SKELETON}

make_config_string()
{
	tmp=$CONFIG_STRING
	if [ -n "$CONFIG_STRING" ]; then
		CONFIG_STRING=${tmp}_${1}
	else
		CONFIG_STRING=$1
	fi
}

parameter_init()
{
	while read line
	do
		case "$line" in
			usb_mtp_en)
				MTP_EN=on
				make_config_string mtp
				;;
			usb_adb_en)
				ADB_EN=on
				make_config_string adb
				;;
			usb_ums_en)
				UMS_EN=on
				make_config_string ums
				;;
			usb_ntb_en)
				NTB_EN=on
				make_config_string ntb
				;;
			usb_acm_en)
				ACM_EN=on
				make_config_string acm
				;;
		esac
	done < $DIR/.usb_config

	case "$CONFIG_STRING" in
		ums)
			PID=0x0000
			;;
		mtp)
			PID=0x0001
			;;
		adb)
			PID=0x0006
			;;
		mtp_adb | adb_mtp)
			PID=0x0011
			;;
		ums_adb | adb_ums)
			PID=0x0018
			;;
		acm)
			PID=0x1005
			;;
		*)
			PID=0x0019
	esac
}

configfs_init()
{
	mount -t configfs none ${CONFIGFS_DIR}
	mkdir ${USB_CONFIGFS_DIR} -m 0770
	echo 0x2207 > ${USB_CONFIGFS_DIR}/idVendor
	echo $PID > ${USB_CONFIGFS_DIR}/idProduct
	mkdir ${USB_STRINGS_DIR}   -m 0770
	SERIAL=`cat /proc/cpuinfo | grep Serial | awk '{print $3}'`
	if [ -z $SERIAL ];then
		SERIAL=0123456789ABCDEF
	fi
	echo $SERIAL > ${USB_STRINGS_DIR}/serialnumber
	echo "rockchip"  > ${USB_STRINGS_DIR}/manufacturer
	echo "rk3xxx"  > ${USB_STRINGS_DIR}/product
	mkdir ${USB_CONFIGS_DIR}  -m 0770
	mkdir ${USB_CONFIGS_DIR}/strings/${USB_ATTRIBUTE}  -m 0770
	echo 500 > ${USB_CONFIGS_DIR}/MaxPower
	echo ${CONFIG_STRING} > ${USB_CONFIGS_DIR}/strings/${USB_ATTRIBUTE}/configuration
}

function_init()
{
	if [ $UMS_EN = on ];then
		mkdir ${USB_FUNCTIONS_DIR}/mass_storage.0
		echo /dev/disk/by-partlabel/userdata > ${USB_FUNCTIONS_DIR}/mass_storage.0/lun.0/file
		ln -s ${USB_FUNCTIONS_DIR}/mass_storage.0 ${USB_CONFIGS_DIR}/mass_storage.0
	fi

	if [ $ADB_EN = on ];then
		mkdir ${USB_FUNCTIONS_DIR}/ffs.adb
		ln -s ${USB_FUNCTIONS_DIR}/ffs.adb ${USB_CONFIGS_DIR}/ffs.adb
	fi

	if [ $MTP_EN = on ];then
		mkdir ${USB_FUNCTIONS_DIR}/mtp.gs0
		ln -s ${USB_FUNCTIONS_DIR}/mtp.gs0 ${USB_CONFIGS_DIR}/mtp.gs0
	fi

	if [ $NTB_EN = on ];then
		mkdir ${USB_FUNCTIONS_DIR}/ffs.ntb
		ln -s ${USB_FUNCTIONS_DIR}/ffs.ntb ${USB_CONFIGS_DIR}/ffs.ntb
	fi

	if [ $ACM_EN = on ];then
		mkdir ${USB_FUNCTIONS_DIR}/acm.gs6
		ln -s ${USB_FUNCTIONS_DIR}/acm.gs6 ${USB_CONFIGS_DIR}/acm.gs6
	fi
}

case "$1" in
start)
	DIR=$(cd `dirname $0`; pwd)
	if [ ! -e "$DIR/.usb_config" ]; then
		echo "$0: Cannot find .usb_config"
		exit 0
	fi

	parameter_init
	if [ -z $CONFIG_STRING ]; then
		echo "$0: no function be selected"
		exit 0
	fi
	configfs_init
	function_init

	if [ $ADB_EN = on ];then
		mkdir -p /dev/usb-ffs/adb
		mount -o uid=2000,gid=2000 -t functionfs adb /dev/usb-ffs/adb
		export service_adb_tcp_port=5555
		adbd&
		sleep 1
	fi

	if [ $MTP_EN = on ];then
		if [ $ADB_EN = on ]; then
			mtp-server&
		else
			sleep 1 && mtp-server&
		fi
	fi

	if [ $NTB_EN = on ];then
		mkdir -p /dev/usb-ffs/ntb
		mount -o uid=2000,gid=2000 -t functionfs ntb /dev/usb-ffs/ntb
	fi

	UDC=`ls /sys/class/udc/| awk '{print $1}'`
	echo $UDC > ${USB_CONFIGFS_DIR}/UDC
	;;
stop)
	echo "none" > ${USB_CONFIGFS_DIR}/UDC
	;;
restart|reload)
	;;
*)
	echo "Usage: $0 {start|stop|restart}"
	exit 1
esac

exit 0
